﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Qianwei.Model;
using Qianwei.Dac;
using Qianwei.Common;
using Qianwei.Common.Extension;
using MySql.Data.MySqlClient;

namespace Qianwei.Biz
{
    public class HomePagePictureManager : ManagerBase<HomePagePictureManager>
    {
        public IList<home_page_picture> QueryHomePagePicture(string strWhere)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM home_page_picture ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            IList<home_page_picture> hpmlist = Mapper.DataReaderToList<home_page_picture>(msh.ExecuteReader(strSql.ToString()));
            return hpmlist;
        }

        public IList<home_page_picture> QueryHomePagePicture(string strWhere, string filedOrder)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM home_page_picture ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            IList<home_page_picture> hpmlist = Mapper.DataReaderToList<home_page_picture>(msh.ExecuteReader(strSql.ToString()));
            return hpmlist;
        }

        public home_page_picture GetModel(int home_Page_Id)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Home_Page_Id, Sort_Id, Picture_Path, Picture_Des, Page_Content, Create_Time, Last_Change_Time  ");
            strSql.Append("  from home_page_picture ");
            strSql.Append(" where Home_Page_Id=@Home_Page_Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Home_Page_Id", (object)home_Page_Id)
            };

            home_page_picture hpp = msh.ExecuteReader(strSql.ToString(), parameters).DataReaderToObject<home_page_picture>();
            return hpp;
        }

        public bool Delete(int home_Page_Id, string webPath)
        {
            home_page_picture hpp = GetModel(home_Page_Id);
            if (hpp != null && File.Exists(string.Format("{0}\\{1}", webPath, hpp.Picture_Path)))
            {
                File.Delete(string.Format("{0}\\{1}", webPath, hpp.Picture_Path));
            }
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from home_page_picture ");
            strSql.Append(" where Home_Page_Id=@Home_Page_Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Home_Page_Id", (object)home_Page_Id)
            };

            int rows = msh.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int Add(home_page_picture model)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into home_page_picture(");
            strSql.Append("Sort_Id,Picture_Path,Picture_Des,Page_Content,Create_Time,Last_Change_Time");
            strSql.Append(") values (");
            strSql.Append("@Sort_Id,@Picture_Path,@Picture_Des,@Page_Content,@Create_Time,@Last_Change_Time");
            strSql.Append(") ");
            strSql.Append(";SELECT LAST_INSERT_ID();");
            MySqlParameter[] parameters = {
			            new MySqlParameter("@Sort_Id", (object)model.Sort_Id) ,            
                        new MySqlParameter("@Picture_Path", model.Picture_Path) ,            
                        new MySqlParameter("@Picture_Des", model.Picture_Des) ,            
                        new MySqlParameter("@Page_Content", model.Page_Content) ,            
                        new MySqlParameter("@Create_Time", model.Create_Time) ,            
                        new MySqlParameter("@Last_Change_Time", model.Last_Change_Time)             
              
            };

            object obj = msh.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
